package cn.wolfcode.trip.admin.Controller;


import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 攻略文章资源控制器
 */
@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Autowired
    private IStrategyDetailService detailService;

    @Autowired
    private IStrategyService strategyService;

    /**
     * 查出所有攻略的文章
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String query(@ModelAttribute("qo") QueryObject qo, Model model){
        qo.setOrderBy("sc.id, sd.sequence");
        model.addAttribute("strategys",strategyService.listAll());
        PageInfo<StrategyDetail> pageInfo= detailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/strategyDetail/list";
    }

    /**
     * 攻略文章保存和修改
     * @param strategyDetail
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(StrategyDetail strategyDetail){
        JSONResult jsonResult =new JSONResult();

        try {
            detailService.saveOrUpdate(strategyDetail);
        } catch (Exception e) {
            jsonResult.mark("操作失败,请稍后再试");
            e.printStackTrace();
        }

        return jsonResult;
    }

    /**
     * 根据攻略文章id获取攻略内容
     * @param id
     * @return
     */
    @RequestMapping("/getStrategyContentById")
    @ResponseBody
    public StrategyContent getStrategyContentById(Long id){
        return detailService.getStrategyContentById(id);
    }
}
